package com.tianyang.yukuaifu.business.auth.view;

import android.content.Context;
import android.net.Uri;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.facebook.drawee.view.SimpleDraweeView;
import com.tianyang.yukuaifu.R;
import com.tianyang.yukuaifu.business.auth.bean.AuthMerchantOutput;
import com.tianyang.yukuaifu.business.auth.bean.BusinessOutput;
import com.tianyang.yukuaifu.business.auth.bean.MerchantBasicInput;
import com.tianyang.yukuaifu.business.auth.bean.SampleImageOutput;
import com.tianyang.yukuaifu.business.auth.contract.AuthConfig;
import com.tianyang.yukuaifu.common.cache.CacheManage;
import com.tianyang.yukuaifu.common.cache.CacheModel;
import com.tianyang.yukuaifu.common.interfaces.CommonConfig;
import com.tianyang.yukuaifu.module.dialog.SampleImageDialog;
import com.tianyang.yukuaifu.utils.SoftInputUtil;
import com.tianyang.yukuaifu.utils.ToastUtil;
import com.tianyang.yukuaifu.utils.VerifyUtil;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Description：小微商户基本信息
 * Created by xyj on 2024/3/10
 */
public class AuthMerchantBasicView extends LinearLayout {

    @BindView(R.id.shopHeadImgSdv)
    SimpleDraweeView shopHeadImgSdv;
    @BindView(R.id.shopOutImgSdv)
    SimpleDraweeView shopOutImgSdv;
    @BindView(R.id.shopDeskImgSdv)
    SimpleDraweeView shopDeskImgSdv;
    @BindView(R.id.mchNameEt)
    EditText mchNameEt;
    @BindView(R.id.nameEt)
    EditText nameEt;
    @BindView(R.id.industryTypeOneTv)
    TextView industryTypeOneTv;
//    @BindView(R.id.industryAliPayTv)
//    TextView industryAliPayTv;
//    @BindView(R.id.industryWechatTv)
//    TextView industryWechatTv;
    @BindView(R.id.placeCodeTextTv)
    TextView placeCodeTextTv;
    @BindView(R.id.merchantAddressEt)
    EditText merchantAddressEt;
//    @BindView(R.id.emailEt)
//    EditText emailEt;
//    @BindView(R.id.remarkEt)
//    EditText remarkEt;
    @BindView(R.id.nextStepTv)
    Button nextStepTv;

    @BindView(R.id.otherAttachmentLl)
    LinearLayout otherAttachmentLl;
    @BindView(R.id.otherAttachmentImgSdv)
    SimpleDraweeView otherAttachmentImgSdv;

    private SampleImageDialog vSampleImageDialog;

    private MerchantBasicInput mMerchantBasicInput;

    private SampleImageOutput mOutput;

    public AuthMerchantBasicView(Context context, AttributeSet attrs) {
        super(context, attrs);
        View.inflate(getContext(), R.layout.view_auth_merchant_basic, this);
        ButterKnife.bind(this);
        mMerchantBasicInput = new MerchantBasicInput();
        mOutput = CacheManage.getInstance().getCache(CacheModel.SAMPLE_IMAGE);
    }

    @OnClick({R.id.takeShopHeadPicTv, R.id.takeShopOutPicTv, R.id.takeShopDeskTv, R.id.otherAttachmentTv})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.takeShopHeadPicTv:
                if (mOutput != null) {
                    showSampleImageDialog(mOutput.getXiaoweishopHeadPic());
                }
                break;
            case R.id.takeShopOutPicTv:
                if (mOutput != null) {
                    showSampleImageDialog(mOutput.getXiaoweishopOnPic());
                }
                break;
            case R.id.takeShopDeskTv:
                if (mOutput != null) {
                    showSampleImageDialog(mOutput.getXiaoweishopCashPic());
                }
                break;
            case R.id.otherAttachmentTv:
                if (mOutput != null) {
                    showSampleImageDialog(mOutput.getShopLocationPic());
                }
                break;
        }
    }

    private void showSampleImageDialog(SampleImageOutput.SampleImage sampleImage) {
        if (vSampleImageDialog == null) {
            vSampleImageDialog = new SampleImageDialog(getContext());
        }
        vSampleImageDialog.showPictureDataView(sampleImage);
        vSampleImageDialog.show();
    }

    public void onFeedBackView(AuthMerchantOutput output, String posChannel) {
        if (TextUtils.equals(AuthConfig.POS_CHANNEL_YS, posChannel)) {
            otherAttachmentLl.setVisibility(View.VISIBLE);
        } else {
            otherAttachmentLl.setVisibility(View.INVISIBLE);
        }
        if (output != null) {
            mMerchantBasicInput = JSON.parseObject(JSON.toJSONString(output), MerchantBasicInput.class);
            if (mMerchantBasicInput != null) {
                if (!TextUtils.isEmpty(mMerchantBasicInput.getShopHeadPic()))
                    shopHeadImgSdv.setImageURI(Uri.parse(mMerchantBasicInput.getShopHeadPic()));
                if (!TextUtils.isEmpty(mMerchantBasicInput.getShopOnPic()))
                    shopOutImgSdv.setImageURI(Uri.parse(mMerchantBasicInput.getShopOnPic()));
                if (!TextUtils.isEmpty(mMerchantBasicInput.getShopCashPic()))
                    shopDeskImgSdv.setImageURI(Uri.parse(mMerchantBasicInput.getShopCashPic()));
                if (!TextUtils.isEmpty(mMerchantBasicInput.getOtherAttachment()))
                    otherAttachmentImgSdv.setImageURI(Uri.parse(mMerchantBasicInput.getOtherAttachment()));
                mchNameEt.setText(VerifyUtil.checkString(mMerchantBasicInput.getMchName()));
                nameEt.setText(VerifyUtil.checkString(mMerchantBasicInput.getName()));
                industryTypeOneTv.setText(VerifyUtil.checkString(mMerchantBasicInput.getIndustryTypeOne()));
//                if (!TextUtils.isEmpty(mMerchantBasicInput.getAliPayMcc())) {
//                    String[] aliPayMcc = mMerchantBasicInput.getAliPayMcc().split("_");
//                    industryAliPayTv.setText(aliPayMcc[0]);
//                    mMerchantBasicInput.setIndustryAliPay(aliPayMcc[0]);
//                }
//                if (!TextUtils.isEmpty(mMerchantBasicInput.getWechatMcc())) {
//                    String[] wechatMcc = mMerchantBasicInput.getWechatMcc().split("_");
//                    industryWechatTv.setText(wechatMcc[0]);
//                    mMerchantBasicInput.setIndustryWechat(wechatMcc[0]);
//                }
                if (!TextUtils.isEmpty(mMerchantBasicInput.getPlaceProvinceName())) {
                    String placeCodeText = VerifyUtil.checkString(mMerchantBasicInput.getPlaceProvinceName())
                            + " " + VerifyUtil.checkString(mMerchantBasicInput.getPlaceCityName())
                            + " " + VerifyUtil.checkString(mMerchantBasicInput.getPlaceAreaName());
                    placeCodeTextTv.setText(placeCodeText);
                }
                merchantAddressEt.setText(VerifyUtil.checkString(mMerchantBasicInput.getMerchantAddress()));
//                emailEt.setText(VerifyUtil.checkString(mMerchantBasicInput.getEmail()));
//                remarkEt.setText(VerifyUtil.checkString(mMerchantBasicInput.getRemark()));
            } else {
                mMerchantBasicInput = new MerchantBasicInput();
            }
        }
    }

    public void showBusinessView(BusinessOutput output, String mccType) {
        if (output != null) {
            switch (mccType) {
                case AuthConfig.MCC_TYPE_BUSINESS:
                    mMerchantBasicInput.setIndustryTypeOne(output.getMccName());
                    mMerchantBasicInput.setMcc(output.getMcc());
                    industryTypeOneTv.setText(VerifyUtil.checkString(output.getMccName()));
                    break;
                case AuthConfig.MCC_TYPE_ALI:
                    mMerchantBasicInput.setIndustryAliPay(output.getTypeThree());
                    mMerchantBasicInput.setAliPayMcc(output.getTypeThree() + "_" + output.getMccCode());
//                    industryAliPayTv.setText(VerifyUtil.checkString(output.getTypeThree()));
                    break;
                case AuthConfig.MCC_TYPE_WX:
                    mMerchantBasicInput.setIndustryWechat(output.getTypeThree());
                    mMerchantBasicInput.setWechatMcc(output.getTypeThree() + "_" + output.getMccId());
//                    industryWechatTv.setText(VerifyUtil.checkString(output.getTypeThree()));
                    break;
            }
        }
    }

    public void showProvCityAreaView(String provCityName, String wheelCode) {
        if (!TextUtils.isEmpty(provCityName)) {
            String[] provCity = provCityName.split(" ");
            mMerchantBasicInput.setPlaceProvinceName(provCity[0]);
            mMerchantBasicInput.setPlaceCityName(provCity[1]);
            if (provCity.length > 2)
                mMerchantBasicInput.setPlaceAreaName(provCity[2]);
            String[] wheelCodes = wheelCode.split(" ");
            mMerchantBasicInput.setPlaceProvinceCode(wheelCodes[0]);
            mMerchantBasicInput.setPlaceCityCode(wheelCodes[1]);
            if (wheelCodes.length > 2)
                mMerchantBasicInput.setPlaceAreaCode(wheelCodes[2]);
        }
    }

    public void showPhotoView(String urlStr, String photoType) {
        switch (photoType) {
            case CommonConfig.TYPE_SHOPHEAD:
                shopHeadImgSdv.setImageURI(Uri.parse(urlStr));
                mMerchantBasicInput.setShopHeadPic(urlStr);
                break;
            case CommonConfig.TYPE_SHOPOUT:
                shopOutImgSdv.setImageURI(Uri.parse(urlStr));
                mMerchantBasicInput.setShopOnPic(urlStr);
                break;
            case CommonConfig.TYPE_SHOPDESK:
                shopDeskImgSdv.setImageURI(Uri.parse(urlStr));
                mMerchantBasicInput.setShopCashPic(urlStr);
                break;
            case CommonConfig.TYPE_OTHER_ATTACHMENT:
                otherAttachmentImgSdv.setImageURI(Uri.parse(urlStr));
                mMerchantBasicInput.setOtherAttachment(urlStr);
                break;
        }
    }

    public MerchantBasicInput getMerchantBasicInput() {
        mMerchantBasicInput.setMchName(mchNameEt.getText().toString());
        mMerchantBasicInput.setName(nameEt.getText().toString());
        mMerchantBasicInput.setMerchantAddress(merchantAddressEt.getText().toString());
//        mMerchantBasicInput.setEmail(emailEt.getText().toString());
//        mMerchantBasicInput.setRemark(remarkEt.getText().toString());
        return mMerchantBasicInput;
    }

    public boolean checkMerchantBasic() {
        if (TextUtils.isEmpty(mMerchantBasicInput.getShopHeadPic())) {
            ToastUtil.show("请上传门头照");
            return false;
        }
        if (TextUtils.isEmpty(mMerchantBasicInput.getShopOnPic())) {
            ToastUtil.show("请上传店内环境照");
            return false;
        }
        if (TextUtils.isEmpty(mMerchantBasicInput.getShopCashPic())) {
            ToastUtil.show("请上传收银台照");
            return false;
        }
        if (TextUtils.isEmpty(mchNameEt.getText())) {
            ToastUtil.show("请输入商户全称");
            SoftInputUtil.openKeybord(mchNameEt, getContext());
            return false;
        }
        if (TextUtils.isEmpty(nameEt.getText())) {
            ToastUtil.show("请输入商户简称");
            SoftInputUtil.openKeybord(nameEt, getContext());
            return false;
        }
        if (TextUtils.isEmpty(mMerchantBasicInput.getIndustryTypeOne())) {
            ToastUtil.show("请选择一级分类");
            return false;
        }
//        if (TextUtils.isEmpty(mMerchantBasicInput.getAliPayMcc())) {
//            ToastUtil.show("请选择支付宝行业");
//            return false;
//        }
//        if (TextUtils.isEmpty(mMerchantBasicInput.getWechatMcc())) {
//            ToastUtil.show("请选择微信行业");
//            return false;
//        }
        if (TextUtils.isEmpty(mMerchantBasicInput.getPlaceProvinceCode())) {
            ToastUtil.show("请选择地区码");
            return false;
        }
        if (TextUtils.isEmpty(merchantAddressEt.getText())) {
            ToastUtil.show("请填写营业执照详细地址");
            SoftInputUtil.openKeybord(merchantAddressEt, getContext());
            return false;
        }
//        if (TextUtils.isEmpty(emailEt.getText())) {
//            ToastUtil.show("请输入电子邮箱");
//            SoftInputUtil.openKeybord(emailEt, getContext());
//            return false;
//        }
//        if (TextUtils.isEmpty(remarkEt.getText())) {
//            ToastUtil.show("请输入备注");
//            SoftInputUtil.openKeybord(remarkEt, getContext());
//            return false;
//        }
        return true;
    }

    public void setOnlyReadView() {
        mchNameEt.setEnabled(false);
        nameEt.setEnabled(false);
        industryTypeOneTv.setEnabled(false);
        industryTypeOneTv.setCompoundDrawables(null, null, null, null);
//        industryAliPayTv.setEnabled(false);
//        industryAliPayTv.setCompoundDrawables(null, null, null, null);
//        industryWechatTv.setEnabled(false);
//        industryWechatTv.setCompoundDrawables(null, null, null, null);
        placeCodeTextTv.setEnabled(false);
        placeCodeTextTv.setCompoundDrawables(null, null, null, null);
        merchantAddressEt.setEnabled(false);
//        emailEt.setEnabled(false);
//        remarkEt.setEnabled(false);
        nextStepTv.setVisibility(GONE);
    }

    public void onDestroyView() {
        if (vSampleImageDialog != null && vSampleImageDialog.isShowing()) {
            vSampleImageDialog.dismiss();
            vSampleImageDialog = null;
        }
    }
}
